// 引入Three.js
import * as THREE from "three";
// 引入Three.js扩展库
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

// 01.创建一个三位场景
const scene = new THREE.Scene();

// 创建设备材质
// const material = new THREE.MeshLambertMaterial({
//    color: 0x00bfff,
//    transparent: true, //开启透明
//    opacity: 0.5, //设置透明度
// });

let material= new THREE.MeshStandardMaterial({
  
   roughness:.2,
   metalness: 1,
   color:new THREE.Color('#d84c21'),
   side: THREE.DoubleSide,
 }) 

// 创建平面材质
/* const materialground = new THREE.MeshLambertMaterial({
	color: 0xcccccc,
}); */

// 创建平面
/* const plane = SHAPE.AddGround(100, 100, -2, materialground);
scene.add(plane); */

// 创建轴网
const grid = new THREE.GridHelper(150, 10, 0x005081, 0x005081);
// grid.rotateOnAxis(new THREE.Vector3(1, 0, 0), 90 * (Math.PI / 180));
scene.add(grid);

// 05.创建一个环境光
// const ambient = new THREE.AmbientLight(0xffffff, 1);
// scene.add(ambient);

let pointLight = new THREE.DirectionalLight(0xffffff, .1);
// pointLight.position.set(50, 50, 50);
// pointLight.target.set(0,0,0)
// pointLight.angle = Math.PI 
scene.add(pointLight);

// 06.AxesHelper: 添加辅助观察的坐标系
// const axesHelper = new THREE.AxesHelper(100);
// scene.add(axesHelper);

// 创建一个左手坐标系替换threejs自带的右手坐标系
var xAxesMaterial = new THREE.LineBasicMaterial({ color: 0xff0000 });
var yAxesMaterial = new THREE.LineBasicMaterial({ color: 0x00ff00 });
var zAxesMaterial = new THREE.LineBasicMaterial({ color: 0x0000ff });

//这里用这个构造
const xAxesGeometry = new THREE.BufferGeometry();
const yAxesGeometry = new THREE.BufferGeometry();
const zAxesGeometry = new THREE.BufferGeometry();

const xLinePointsArray = new Array();
const yLinePointsArray = new Array();
const zLinePointsArray = new Array();
//用点组成线
for (let i = 0; i < 90; i++) {
   const x = Math.random() * i - 1;
   const y = Math.random() * i - 1;
   const z = Math.random() * i - 1;
   xLinePointsArray.push(new THREE.Vector3(x, 0, 0));
   yLinePointsArray.push(new THREE.Vector3(0, y, 0));
   zLinePointsArray.push(new THREE.Vector3(0, 0, -z));
}
//用这个api传入顶点数组
xAxesGeometry.setFromPoints(xLinePointsArray);
yAxesGeometry.setFromPoints(yLinePointsArray);
zAxesGeometry.setFromPoints(zLinePointsArray);

var xAxes = new THREE.Line(xAxesGeometry, xAxesMaterial);
var yAxes = new THREE.Line(yAxesGeometry, yAxesMaterial);
var zAxes = new THREE.Line(zAxesGeometry, zAxesMaterial);
scene.add(xAxes);
scene.add(yAxes);
scene.add(zAxes);

// 03.创建一个透视相机
const width = 1000;
const height = 500;
// 45：视场角度，width / height：Canvas画布宽高比，1：相机距近裁截面距离，1000：相机距远裁截面距离
let camera = new THREE.PerspectiveCamera(75, width / height, .1, 10000);
// camera.position.set(100, 150, 100); //设置相机在三维坐标中的位置
// camera.lookAt(0, 150, 0); //设置观察方向


// const camera = new THREE.OrthographicCamera(
//    -width/2, width/2, height/2, -height/2, -800, 800
//  );
camera.position.set(72, 109, 86); //设置相机在三维坐标中的位置
camera.lookAt(0, 0, 0); //设置观察方向

// const viewHelper = new ViewHelper(camera,);

// 04.创建渲染器对象
const render = new THREE.WebGL1Renderer({
   antialias: true,
   alpha: true,
});
render.setSize(width, height); // 设置渲染的尺寸（像素px）
render.render(scene, camera); //指定需渲染的场景和相机，进行渲染
render.localClippingEnabled = true
// 设置相机控件轨道控制器OrbitControls
// const controls = new OrbitControls(camera, render.domElement);
// controls.enableDamping = true;
// render.sortObjects = true; // 默认情况下启用
// camera.depth = 1; // 确保相机有深度缓冲

// 射线检测
const raycaster = new THREE.Raycaster();






export { scene, material, camera, render, raycaster,pointLight };
